<?php 
session_cache_expire(999999999);
if(!isset($_SESSION)) 
{ 
	session_start(); 
}
$lang = $_SESSION['lang'];
if (empty($lang) || $lang == "EN") {
	include "../value/value_en.php";
} else if($lang == "VN") {
	include "../value/value_vn.php";
}
else {
	include "../value/value_kr.php";
}
include '../operations/connection.php';	
$stonggiacungcap=0;
$stongthue=0;
$stongtien=0;
$arr=array();
$mangqui=array("('1','2','3')","('4','5','6')","('7','8','9')","('10','11','12')");

if(isset($_REQUEST['loaitimkiem'])){
	if($_REQUEST['loaitimkiem']=="thang"){
		$ntt=date("t",mktime(0,0,0,$_REQUEST['mon'],1,$_REQUEST['year']));
		if($_REQUEST['mon']<10){
			$vtime=$_REQUEST['nam']."-0".$_REQUEST['thang']."-01"." ~ ".$_REQUEST['nam']."-0".$_REQUEST['thang']."-".$ntt;
		}else{
			$vtime=$_REQUEST['nam']."-".$_REQUEST['thang']."-01"." ~ ".$_REQUEST['nam']."-".$_REQUEST['thang']."-".$ntt;
		}
	}else if($_REQUEST['loaitimkiem']=="qui"){
		$mangquy=array(1,1,1,2,2,2,3,3,3,4,4,4);
		$mq=array(1=>array(1,3),2=>array(4,6),3=>array(7,9),4=>array(10,12));
		$ntt=date("t",mktime(0,0,0,$mq[$_REQUEST['qui']][1],1,$_REQUEST['nam']));
		if($mq[$_REQUEST['qui']][0]<10){
			$vtime=$_REQUEST['nam']."-0".$mq[$_REQUEST['qui']][0]."-01"." ~ ".$_REQUEST['nam']."-0".$mq[$_REQUEST['qui']][1]."-".$ntt;
		}else{
			$vtime=$_REQUEST['nam']."-".$mq[$_REQUEST['qui']][0]."-01"." ~ ".$_REQUEST['nam']."-".$mq[$_REQUEST['qui']][1]."-".$ntt;
		}
	}else if($_REQUEST['loaitimkiem']=="all"){
		if(isset($_REQUEST['nam']))
			$vtime=$_REQUEST['nam']."-01-01"." ~ ".$_REQUEST['nam']."-31-12";
		else $vtime="~";
	}else if($_REQUEST['loaitimkiem']=="ngay"){
			$vtime=$_REQUEST['df']." ~ ".$_REQUEST['df'];
	}
}

if(isset($_REQUEST['action'])){
	$sql=$_SESSION['sqlcontract'];	
	if($_SESSION['ASC_DESC']=='DESC')
		$_SESSION['ASC_DESC']="ASC";
	else $_SESSION['ASC_DESC']="DESC";
	$sql.="order by c.signDay ".$_SESSION['ASC_DESC'];
	//echo $sql;
	$sqlempworking=mysql_query($sql);
}else if(isset($_REQUEST['loaitimkiem'])){
	if($_REQUEST['loaitimkiem']=="qui"){
		$qui=$mangqui[$_REQUEST['qui']-1];
		$sql="SELECT c.*,t.name$lang,e.FullName FROM contract c,contract_type t,employee e WHERE c.type=t.id AND c.signer=e.IDEmployee 
			AND c.status=0 AND c.nameCompany like '%".$_REQUEST['cusname']."%' AND c.nameProduct like '%".$_REQUEST['nameProduct']."%'";
		
		if($_REQUEST['type']!=0)
			$sql .=" and c.type = '".$_REQUEST['type']."'";
		if($_REQUEST['statusPayment']!=-1)
			$sql .=" and c.statusPayment = '".$_REQUEST['statusPayment']."'";
		
		$sql .=" and ( month(c.signDay) in ".$qui." and year(c.signDay) = '".$_REQUEST['nam']."' ) ";
		
		if(isset($_SESSION['sqlcontract']))
			unset($_SESSION['sqlcontract']);
		$_SESSION['sqlcontract']=$sql;	
		$_SESSION['ASC_DESC']='DESC';	
		$sql.="order by c.signDay ".$_SESSION['ASC_DESC'];
		
		
		$sqlempworking=mysql_query($sql);
		
	}
}
$soluong=mysql_num_rows($sqlempworking);
$addressbrowser = $_SERVER['SERVER_NAME'];
$temp = explode(".", $addressbrowser);
?>
			<input type="hidden" id="loaitimkiem" value="<?=$_REQUEST['loaitimkiem']?>"/>
			<table id="listEmp" class="ftable" style="width: 100%;">

				<tr >
					<th  style="width: 30px;min-width: 30px" scope="col"><?=$l_no?></th>
					<!-- <th  style="width: 50px;min-width: 50px" scope="col"><?=$l_type_contract?></th> 
					<th  style="width: 90px;min-width: 90px" scope="col" ><?=$l_code_contract?></th>-->
					<th  style="min-width: 150px;" scope="col"><?=$l_name_contract?></th>
					<th  style="min-width: 90px;" scope="col"><a onclick="sort_signDay()"><?=$l_signDay_contract?></a></th>
					<th  style="min-width: 80px;" scope="col"><?=$l_content_contract?></th>
					<th  style="min-width: 80px;" scope="col"><?=$l_totalValue_contract?></th>
					<th  style="min-width: 80px;" scope="col"><?=$l_prepaidMoney_contract?></th>
					<th  style="min-width: 90px;" scope="col"><?=$l_expectedDay_contract?></th>
					<th  style="min-width: 80px;" scope="col"><?=$l_nameCompany_contract?></th>
					<th  style="min-width: 80px;" scope="col"><?=$l_placeDelivery_contract?></th>
					<th  style="min-width: 80px;" scope="col"><?=$l_sendmail?></th>
				</tr>
				<?php $i =1;
				if(isset($_SESSION['listcontract']))
					unset($_SESSION['listcontract']);
				$listcontract=array();	
				$temp1="";
				$stong1=0;
				$stong2=0;
				$tong1=0;
				$tong2=0;
				if (mysql_num_rows($sqlempworking)>0){
				$row=mysql_fetch_array($sqlempworking);
				?>
					<tr>
					<td  style="font-size: 12px;text-align: center;color: #111111;" scope="col"><?php echo $i;?></td>
					<!--  <td  style="font-size: 12px;text-align: center;color: #111111;" scope="col"><?php echo $row['name'.$lang];?></td>
					<td  style="font-size: 12px;text-align: center;" scope="col"><a href="contract.php?content=content_editcontract&code=<?=$row['code']?>" ><?php echo $row['code'];?></a></td>-->
					<td  style="font-size: 12px;text-align: left;" scope="col" ><a href="contract.php?content=content_editcontract&code=<?=$row['code']?>" ><?php echo $row['name'];?></a></td>
					<td  style="font-size: 12px;text-align: center;color: #111111;" scope="col"><?php echo $row['signDay'];?></td>
					<td  style="font-size: 12px;text-align: left;color: #111111;" scope="col"><?php echo $row['content'];?></td>
					<td  style="font-size: 12px;text-align: right;color: #111111;padding-right: 5px" scope="col"><span <?php if($row['statusPayment']==1) echo 'style="color: red"';?>><?php if($row['totalValue']!=0)echo number_format($row['totalValue']);?></span></td>
					<td  style="font-size: 12px;text-align: right;color: #111111;padding-right: 5px" scope="col"><?php if($row['prepaidMoney']!=0)echo number_format($row['prepaidMoney']);?></td>
					<td  style="font-size: 12px;text-align: center;color: #111111;" scope="col"><?php echo $row['expectedDay'];?></td>
					<td  style="font-size: 12px;text-align: left;color: #111111;" scope="col"><?php echo $row['nameCompany'];?></td>
					<td  style="font-size: 12px;text-align: left;color: #111111;" scope="col"><?php echo $row['placeDelivery'];?></td>
					<td><img src="images/email.png"  onclick="setdata('<?=$row['code']?>','<?=$lang?>','<?=$temp[0]?>')" title="<?=$l_sendmail?>" /></td>
					</tr>
				<?php 
				$d=1;
				$temp1=substr($row['signDay'], 0,7);
				$tong1+=$row['totalValue'];
					$tong2+=$row['prepaidMoney'];
				$listcontract[$temp1][]=array($row['name'.$lang],$row['code'],$row['name'],$row['signDay'],$row['totalValue'],
				 									  $row['prepaidMoney'],$row['otherMoney'],$row['statusPayment'],$row['FullName'],$row['expectedDay'],$row['completeDay'],
				 									  $row['area'],$row['placeDelivery'],$row['nameCompany'], $row['agencies'],$row['provideGovernment'],$row['method1'],
				 									  $row['content'],$row['method2'],$row['provide'], $row['nameProduct'],$row['promotions'],$row['expireWarrantyDay'],
				 									  $row['agenciesCheck'],$row['methodsPayment'], $row['methodsDelivery'],$row['classification'],$row['comment']);
					while ($row=mysql_fetch_array($sqlempworking)){ 
				 	$i++ ;
				 	if($temp1!=substr($row['signDay'], 0,7)) {
						?>
					<tr class="coltr2" style="font-weight:bold;font-size:14px;">
		                <td colspan="2"  style="padding: 10px;color:#111111 ">
							<?php 
							$stong1+=$tong1;
							$stong2+=$tong2;
								if($_SESSION['loaitimkiem']=="thang"){
									?>
									<?php echo $l_month;?> : <font color="red"><?php echo substr($temp1, 0,7); ?></font>  -  <?php echo $l_co;?>  <font color="blue"><?php echo number_format($d);?></font> <?php echo $l_contract;?> 
									<?php 
								}else if($_SESSION['loaitimkiem']=="qui"){
									?>
									<?php echo $l_month;?> : <font color="red"><?php echo substr($temp1, 0,7); ?></font>  -  <?php echo $l_co;?>  <font color="blue"><?php echo number_format($d);?></font> <?php echo $l_contract;?>
									<?php 
								}else if($_SESSION['loaitimkiem']=="all"){
									
									?>
									<?php echo $l_month;?> : <font color="red"><?php echo substr($temp1, 0,7); ?></font>  -  <?php echo $l_co;?>  <font color="blue"><?php echo number_format($d);?></font> <?php echo $l_contract;?> 
									<?php 
								}
							?>
						</td>
		                <td style="text-align:right;"></td>
		                <td style="text-align:right;"></td>
		                <td style="text-align:right;padding-right: 5px;color:#111111"><?php echo number_format($tong1);?></td>
		                <td style="text-align:right;padding-right: 5px;color:#111111"><?php echo number_format($tong2);?></td>
		                <td></td>
		                <td></td>
		                <td></td>
		                <td></td>
		            </tr>
					<?php 
				 	$d=0;
				 	$temp1=substr($row['signDay'], 0,7);
				 	$tong1=0;
				 	$tong2=0;
				 	}?>
					<tr>
					<td  style="font-size: 12px;text-align: center;color: #111111;" scope="col"><?php echo $i;?></td>
					<!--  <td  style="font-size: 12px;text-align: center;color: #111111;" scope="col"><?php echo $row['name'.$lang];?></td>
					<td  style="font-size: 12px;text-align: center;" scope="col"><a href="contract.php?content=content_editcontract&code=<?=$row['code']?>" ><?php echo $row['code'];?></a></td>-->
					<td  style="font-size: 12px;text-align: left;" scope="col" ><a href="contract.php?content=content_editcontract&code=<?=$row['code']?>" ><?php echo $row['name'];?></a></td>
					<td  style="font-size: 12px;text-align: center;color: #111111;" scope="col"><?php echo $row['signDay'];?></td>
					<td  style="font-size: 12px;text-align: left;color: #111111;" scope="col"><?php echo $row['content'];?></td>
					<td  style="font-size: 12px;text-align: right;color: #111111;padding-right: 5px" scope="col"><span <?php if($row['statusPayment']==1) echo 'style="color: red"';?>><?php if($row['totalValue']!=0)echo number_format($row['totalValue']);?></span></td>
					<td  style="font-size: 12px;text-align: right;color: #111111;padding-right: 5px" scope="col"><?php if($row['prepaidMoney']!=0)echo number_format($row['prepaidMoney']);?></td>
					<td  style="font-size: 12px;text-align: center;color: #111111;" scope="col"><?php echo $row['expectedDay'];?></td>
					<td  style="font-size: 12px;text-align: left;color: #111111;" scope="col"><?php echo $row['nameCompany'];?></td>
					<td  style="font-size: 12px;text-align: left;color: #111111;" scope="col"><?php echo $row['placeDelivery'];?></td>
					<td><img src="images/email.png"  onclick="setdata('<?=$row['code']?>','<?=$lang?>','<?=$temp[0]?>')" title="<?=$l_sendmail?>" /></td>
					</tr>
				<?php 
					$d++;
					$tong1+=$row['totalValue'];
					$tong2+=$row['prepaidMoney'];
					$listcontract[$temp1][]=array($row['name'.$lang],$row['code'],$row['name'],$row['signDay'],$row['totalValue'],
				 									  $row['prepaidMoney'],$row['otherMoney'],$row['statusPayment'],$row['FullName'],$row['expectedDay'],$row['completeDay'],
				 									  $row['area'],$row['placeDelivery'],$row['nameCompany'], $row['agencies'],$row['provideGovernment'],$row['method1'],
				 									  $row['content'],$row['method2'],$row['provide'], $row['nameProduct'],$row['promotions'],$row['expireWarrantyDay'],
				 									  $row['agenciesCheck'],$row['methodsPayment'], $row['methodsDelivery'],$row['classification'],$row['comment']);	
					
					}
				$_SESSION['listcontract']=$listcontract;
				?>
				<tr class="coltr2" style="font-weight:bold;font-size:14px;">
		                <td colspan="2"  style="padding: 10px;color:#111111 ">
							<?php 
							$stong1+=$tong1;
							$stong2+=$tong2;
								if($_SESSION['loaitimkiem']=="thang"){
									?>
									<?php echo $l_month;?> : <font color="red"><?php echo substr($temp1, 0,7); ?></font>  -  <?php echo $l_co;?>  <font color="blue"><?php echo number_format($d);?></font> <?php echo $l_contract;?> 
									<?php 
								}else if($_SESSION['loaitimkiem']=="qui"){
									?>
									<?php echo $l_month;?> : <font color="red"><?php echo substr($temp1, 0,7); ?></font>  -  <?php echo $l_co;?>  <font color="blue"><?php echo number_format($d);?></font> <?php echo $l_contract;?>
									<?php 
								}else if($_SESSION['loaitimkiem']=="all"){
									
									?>
									<?php echo $l_month;?> : <font color="red"><?php echo substr($temp1, 0,7); ?></font>  -  <?php echo $l_co;?>  <font color="blue"><?php echo number_format($d);?></font> <?php echo $l_contract;?> 
									<?php 
								}
							?>
						</td>
		                
		                <td style="text-align:right;"></td>
		                <td style="text-align:right;"></td>
		                <td style="text-align:right;padding-right: 5px;color:#111111"><?php echo number_format($tong1);?></td>
		                <td style="text-align:right;padding-right: 5px;color:#111111"><?php echo number_format($tong2);?></td>
		                <td></td>
		                <td></td>
		                <td></td>
		                <td></td>
		            </tr>
		           	<tr style="font-weight:bold;font-size:15px;color:green;">
				        <td colspan="4" style="padding: 10px;"><?php echo $l_tongdon?></td>
		                
		                <td style="text-align:right;padding-right: 5px;color:#111111"><?php echo number_format($stong1);?></td>
		                <td style="text-align:right;padding-right: 5px;color:#111111"><?php echo number_format($stong2);?></td>
		                <td></td>
		                <td></td>
		                <td></td>
		                <td></td>
		            </tr>
				<?php }
				else{
				?>
				<tr>
				<td colspan="10"><?=$l_list_emplty?></td>
				</tr>
				<?php }?>
		</table>
        		<?php 
        		if($soluong>0){
        			?>
        			<div class="fbox2" style="text-align:center; width:100%;">
        				<a href="printer/export_contract.php?typeSearch=all&type=<?php echo $_REQUEST['type'];?>&statusPayment=<?php echo $_REQUEST['statusPayment'];?>&cusname=<?php echo $_REQUEST['cusname'];?>&nameProduct=<?php echo $_REQUEST['nameProduct'];?>&vtime=<?php echo $vtime; ?>"><input type="button" value="<?=$l_export_excel?>" class="btnupbg btnupload" style="background: #4f81bd;"/></a>
        			</div>
        			<?php	
        		}
        		?>
        		
<?php mysql_close($connection);?>